using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Forms;
using SG.Sys.Console.View;

namespace SG.Sys.Console.Cntrl
{
    public class Composer
    {
        private static readonly AggregateCatalog StandardCatalog;

        private readonly CompositionContainer _container;

        static Composer()
        {
            
            //An aggregate catalog that combines multiple catalogs
            StandardCatalog = new AggregateCatalog();
            //Adds all the parts found in the same assembly as the Program class
            StandardCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MainConsole).Assembly));    
        }

        public Composer(params object[] attributedParts)
        {
            //Create the CompositionContainer with the parts in the catalog
            _container = new CompositionContainer(StandardCatalog);

            //Fill the imports of this object
            try
            {
                _container.ComposeParts(attributedParts);
            }
            catch (CompositionException compositionException)
            {
                MessageBox.Show(compositionException.ToString());
            }    
        }
    }
}